package com.baton.plugins.finder;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

/**
 * Servlet implementation class UpServlet
 */
@WebServlet("/upFinder.java")
public class UpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public UpServlet() {
        super();
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init() throws ServletException {
		ServletContextFactory.setServletContext(getServletContext());
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		getResponse(request, response);
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	private void getResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		Map<String,Object> map = new HashMap<String,Object>();
		String command = request.getParameter("command");
		String basePath =  getServletContext().getRealPath("/userfiles/grandid/userid");
		String id = request.getParameter("id");
		id = URLDecoder.decode(id, "UTF-8").replaceAll("\\\\", "/"); 
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		type = URLDecoder.decode(type, "UTF-8").replaceAll("\\\\", "/"); 
		System.out.println(type);
		if(id.indexOf(".") != -1){
			
		}else{
			String oldPath = getServletContext().getRealPath("/userfiles/grandid/userid"  + "/"+type);
			String newPath = getServletContext().getRealPath("/userfiles/grandid/userid"  + "/"+id);
			File file = new File(oldPath);
		boolean newName = 	file.renameTo(new File(newPath));
			if(newName){
				map.put("success", true);
				map.put("msg", "修改成功");
			}
		}
		out.println(JSON.toJSONString(map));

	}
}
